//
//  CHHomeVisitorView.swift
//  status
//
//  Created by 彳亍 on 2017/3/22.
//  Copyright © 2017年 彳亍. All rights reserved.
//

import UIKit
import SnapKit

class CHHomeVisitorView: CHVisitorView {
    
    /// 旋转视图
    fileprivate lazy var rotationImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 蒙版
    fileprivate lazy var coverImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// house 视图
    fileprivate lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 标语label
    fileprivate lazy var sloganLabel:UILabel = UILabel(text: nil, textColor: UIColor.darkGray ,fontSize:12)
    /// 关注按钮
    fileprivate lazy var attentionButton:UIButton = UIButton(title: "去关注",
                                                             titleColor: UIColor.orange,
                                                             backgroundImage: UIImage(named:"common_button_white_disable"),
                                                             target: self,
                                                             action: #selector(attention))
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置UI
        setupUI()
        // 设置动画
        setupAniamtion()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 关注
    override func attention() {
        super.attention()
    }
    
    /// 重写 visitorInfo
    override var visitorInfo: [String : String]? {
        didSet {
            sloganLabel.text = visitorInfo?["message"]
        }
    }

}

// MARK: - UI
extension CHHomeVisitorView {
    
    /// 设置UI
    fileprivate func setupUI() {
        // 1. 添加视图
        addSubview(rotationImageView)
        insertSubview(coverImageView, aboveSubview: rotationImageView)
        insertSubview(houseImageView, aboveSubview: coverImageView)
        addSubview(sloganLabel)
        addSubview(attentionButton)
        
        // 2. 布局
        rotationImageView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.centerY.equalToSuperview().offset(-80)
        }
        
        coverImageView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.left.right.equalToSuperview()
            ConstraintMaker.bottom.equalToSuperview().offset(-220)
        }
        
        houseImageView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.center.equalTo(rotationImageView)
        }
        
        sloganLabel.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.top.equalTo(rotationImageView.snp.bottom)
        }
        
        attentionButton.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.top.equalTo(sloganLabel.snp.bottom).offset(16)
            ConstraintMaker.width.equalTo(120)
        }
    }
    
    /// 设置动画
    fileprivate func setupAniamtion() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 20
        animation.fromValue = 0
        animation.toValue = M_PI * 2
        animation.repeatCount = HUGE
        animation.isRemovedOnCompletion = false
        rotationImageView.layer.add(animation, forKey: nil)
    }

}
